//
//  ShenHeViewController.m
//  Iderun
//
//  Created by  on 12-6-16.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "settingViewController.h"
#import "AppDelegate.h"
#import "MBProgressHUD.h"
#import <QuartzCore/QuartzCore.h>
#import "MasterViewController.h"
#import "AuditedViewController.h"

@implementation settingViewController
@synthesize content;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
            self.edgesForExtendedLayout = UIRectEdgeNone;
        }

        self.title=@"系统配置信息";
        self.navigationItem.hidesBackButton=YES;
        UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(10, 50, 150, 30)];
        label.font=[UIFont  fontWithName:@"STHeitiTC-Medium" size:14];
        label.textColor=[UIColor  blackColor];
        label.backgroundColor=[UIColor  clearColor];
        label.text=@"Web服务地址：";
        [self.view addSubview:label];
        [label release];
        self.view.backgroundColor=[UIColor  grayColor];
        UITextField *textView=[[UITextField alloc] initWithFrame:CGRectMake(10, 80, 300, 31)];
        textView.backgroundColor=[UIColor  whiteColor];
        textView.layer.cornerRadius = 8;//设置那个圆角的有多圆
        textView.layer.borderWidth = 2;//设置边框的宽度，当然可以不要
        textView.layer.borderColor = [[UIColor whiteColor] CGColor];//设置边框的颜色
        textView.layer.masksToBounds = YES;//设为NO去试试
        self.content=textView;
        [textView  release];
        [self.view addSubview:self.content];
        
        NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];
        NSString *url=[userdefault objectForKey:@"kServiceURL"];
        if (nil!=url&&![@"" isEqualToString:url]) {
            self.content.text=url;
            UIBarButtonItem *leftButton=[[UIBarButtonItem alloc] initWithCustomView:[self customButtom:100 withFrame:CGRectMake(10, 0, 49, 29) action:@selector(close:) normalImage:@"back_button.png" lightedImage:@"back_button.png" title:@""]];
            leftButton.style = UIBarButtonItemStylePlain;
            self.navigationItem.leftBarButtonItem=leftButton;
            [leftButton release];
        }else{
            self.content.text=@"http://lrb888.vicp.cc:809";
            //self.content.text=@"http://lrb888.vicp.cc:8010";

            self.navigationItem.leftBarButtonItem=nil;
        }

        [self.content  becomeFirstResponder];
    }
    return self;
}

- (void)viewDidLoad{
    [super viewDidLoad];
   	// Do any additional setup after loading the view.
   
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:[self customButtom:101 withFrame:CGRectMake(10, 0, 43, 29) action:@selector(savesetttingaction:) normalImage:@"big_save.png" lightedImage:@"big_save.png" title:@""]];
    rightButton.style = UIBarButtonItemStylePlain;
    self.navigationItem.rightBarButtonItem=rightButton;
    [rightButton release];
    
    
}

-(IBAction) close:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}



-(IBAction) savesetttingaction:(id)sender{
    NSString *mainUrl=[self.content.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (mainUrl!=nil&&![mainUrl isEqualToString:@""]) {
        NSString *iniUrl=[NSString stringWithFormat:@"%@/%@",mainUrl,@"ini.txt"];
        /*NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:iniUrl]];
        NSString *iniContent=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];*/
       //NSError *error;
        NSString *iniContent=[NSString stringWithContentsOfURL:[NSURL URLWithString:iniUrl] encoding:NSASCIIStringEncoding error:nil];
        if (!iniContent||iniContent==nil) {
            NSString* okTitle = @"提醒";
            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:nil message:@"客户端初始化出错" delegate:nil cancelButtonTitle:okTitle otherButtonTitles:nil];
            [alert show];
            [alert release];
            return;
        }

        NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];
        if (iniContent!=nil&&![iniContent isEqualToString:@""]) {
            [userdefault setObject:mainUrl forKey:@"kServiceURL"];
            [userdefault setObject:iniContent forKey:@"sysParams"];
            UIBarButtonItem *leftButton=[[UIBarButtonItem alloc] initWithCustomView:[self customButtom:100 withFrame:CGRectMake(10, 0, 49, 29) action:@selector(close:) normalImage:@"back_button.png" lightedImage:@"back_button.png" title:@""]];
            leftButton.style = UIBarButtonItemStylePlain;
            self.navigationItem.leftBarButtonItem=leftButton;
            [leftButton release];


        }else {
            [userdefault setObject:nil forKey:@"sysParams"];
        }
        
        
        [userdefault synchronize];
        UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"提醒" message:@"保存成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alertView show];
        [alertView  release];
        
    }else {
        UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"提醒" message:@"请输入Web服务地址" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alertView show];
        [alertView  release];
    }
   
}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    [self.navigationController popToRootViewControllerAnimated:YES];
        
    
}


- (UIButton*) customButtom:(NSUInteger)numberOfTag withFrame:(CGRect)frame action:(SEL)action normalImage:(NSString*)normalImage lightedImage:(NSString*)lightedImage  title:(NSString *)title{
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    button.tag=numberOfTag;
    button.backgroundColor=[UIColor clearColor];
    UIImage *normal_Image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:normalImage ofType:nil]];
    UIImage *lighted_Image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:lightedImage ofType:nil]];
    [button setTitle:title forState:UIControlStateNormal];
    [button setImage:normal_Image forState:UIControlStateNormal];
    [button setImage:lighted_Image forState:UIControlStateHighlighted];
    [button setImage:lighted_Image forState:UIControlStateSelected];
    button.adjustsImageWhenHighlighted = NO;
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation==UIDeviceOrientationPortrait);
}
- (BOOL)shouldAutorotate{
    return NO;
}
- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

-(void)  dealloc{
    [content  release];
    [super dealloc];
}
@end
